/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.eclass.utils;

/**
 *
 * @author fortruth
 */
public class PageNavigation {

    private int total;
    private int step = 10;
    private int showCount = 7;// how much page number buttons to show;
    private boolean showPre = true;//is show previous page button
    private boolean showFirst = true;//is show first page button 
    private boolean showNext = true;//is show next page button
    private boolean showLast = true;//is show last page button
    private String divId = "pageNavigation";//the div id
    private String pageFrom = "pageFrom";// page url name for sql step from
    private String pageStep = "pageStep";//page step count number
    private String baseUrl;
    private int pageCount;

    public PageNavigation(int total, String baseUrl) {
        this.total = total;
        this.baseUrl = baseUrl;
        pageCount = (int) Math.ceil(total / step);
    }
    
    public String echoList() {
        String pageList = ""
                + "<div id='" + divId + "'>"
                + "<div>" + pageCount + "</div>"
                + "<div>" + total + "</div>"
                + "<div>" + getPageList() + "</div>"
                + "</div>";
        return pageList;
    }

    private String getPageList() {
        String pageList = "<ul>";
        String connector = "&";
        if (!baseUrl.contains("?")) {
            connector = "?";
        }
        baseUrl += connector;
        for (int no = 1; no <= pageCount; no++) {
            pageList += "<li><a href='" + baseUrl + pageFrom + "=" + no + "&" + pageStep + "=" + step + "'>" + no + "</a></li>";
        }
        pageList += "</ul>";
        return pageList;
    }

    /**
     * @return the total
     */
    public int getTotal() {
        return total;
    }

    /**
     * @param total the total to set
     */
    public void setTotal(int total) {
        this.total = total;
    }

    /**
     * @return the step
     */
    public int getStep() {
        return step;
    }

    /**
     * @param step the step to set
     */
    public void setStep(int step) {
        this.step = step;
        this.pageCount = (int) Math.ceil(total / step);
    }
}
